package com.vrp3d.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vrp3d.common.consts.CommonConst;
import com.vrp3d.common.enums.CommonExceptionEnum;
import com.vrp3d.common.response.RestResponse;
import com.vrp3d.domain.dto.dist.DistForFrontDTO;
import com.vrp3d.domain.dto.news.*;
import com.vrp3d.service.NewsService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author vrp3d
 */
@Api("新闻")
@Slf4j
@Validated
@RestController
@RequestMapping("/news")
public class NewsController extends AbsController {

    @Autowired
    private NewsService newsService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取新闻分类信息", response = DistForFrontDTO.class)
    @RequestMapping(value = "/getTypeList", method = RequestMethod.GET)
    public RestResponse getTypeList() {
        List<DistForFrontDTO> result = newsService.getTypeList();
        return new RestResponse().put(result);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取新闻列表", response = NewsListDTO.class)
    @ApiImplicitParams({
          @ApiImplicitParam(name = "current", value = "当前页码", required = true, paramType = "query", dataType = "int"),
          @ApiImplicitParam(name = "size", value = "每一页展示数量", required = true, paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getNewsList", method = RequestMethod.GET)
    public RestResponse getNewsList(@ApiIgnore Page<NewsListDTO> page, @Validated NewsSearchDTO search,
                                    @RequestParam(defaultValue = CommonConst.PAGE_CURRENT) Integer current,
                                    @RequestParam(defaultValue = CommonConst.PAGE_SIZE) Integer size) {
        page.setCurrent(current);
        page.setSize(size);
        Page<NewsListDTO> result = newsService.getNewsList(page, search);
        return new RestResponse().put(result);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "判断新闻标题是否可用(新增时)")
    @ApiResponses({
          @ApiResponse(code = 200, message = "新闻标题可用"),
          @ApiResponse(code = 22002, message = "新闻标题已存在")
    })
    @ApiImplicitParam(name = "title", value = "新闻标题", required = true, paramType = "query")
    @RequestMapping(value = "/whetherNewsHeadlinesAreAvailable", method = RequestMethod.GET)
    public RestResponse whetherNewsHeadlinesAreAvailable(@Size(min = 1, max = CommonConst.NEWS_NAME_LENGTH, message = "新闻标题长度限制1-" + CommonConst.NEWS_NAME_LENGTH)
                                                         @NotBlank(message = "展厅名称不能为空") String title) {
        newsService.whetherNewsHeadlinesAreAvailable(title);
        return new RestResponse(CommonExceptionEnum.NEWS_HEADLINES_AVAILABLE.getMsg());
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增新闻")
    @ApiResponses({
          @ApiResponse(code = 200, message = "新增新闻成功"),
          @ApiResponse(code = 22002, message = "新闻标题已存在")
    })
    @RequestMapping(value = "/insertNews", method = RequestMethod.POST)
    public RestResponse insertNews(@Validated @RequestBody NewsInsertDTO dto) {
        newsService.insertNews(dto);
        return new RestResponse(CommonExceptionEnum.NEW_NEWS_SUCCESS.getMsg());
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "判断新闻标题是否可用_修改时")
    @ApiResponses({
          @ApiResponse(code = 200, message = "新闻标题可用"),
          @ApiResponse(code = 22002, message = "新闻标题已存在")
    })
    @ApiImplicitParam(name = "title", value = "新闻标题", required = true, paramType = "query")
    @RequestMapping(value = "/whetherNewsHeadlinesAreAvailableWhenUpdate", method = RequestMethod.GET)
    public RestResponse whetherNewsHeadlinesAreAvailableWhenUpdate(@Size(min = 1, max = CommonConst.NEWS_NAME_LENGTH, message = "新闻标题长度限制1-" + CommonConst.NEWS_NAME_LENGTH)
                                                                   @NotBlank(message = "展厅名称不能为空") String title,
                                                                   @Min(value = 1, message = "id最小值为1") @NotNull(message = "id不能不空") Integer id) {
        newsService.whetherNewsHeadlinesAreAvailableWhenUpdate(id, title);
        return new RestResponse(CommonExceptionEnum.NEWS_HEADLINES_AVAILABLE.getMsg());
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改新闻")
    @ApiResponses({
          @ApiResponse(code = 200, message = "修改新闻成功"),
          @ApiResponse(code = 22002, message = "新闻标题已存在")
    })
    @RequestMapping(value = "/updateNews", method = RequestMethod.PUT)
    public RestResponse updateNews(@Validated @RequestBody NewsUpdateDTO dto) {
        newsService.updateNews(dto);
        return new RestResponse(CommonExceptionEnum.EDIT_NEWS_SUCCESSFULLY.getMsg());
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据id查询", response = NewsDTO.class)
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, paramType = "query")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public RestResponse selectById(@Min(value = 1, message = "id最小值为1") @NotNull(message = "id不能不空") Integer id) {
        NewsDTO result = newsService.selectById(id);
        return new RestResponse().put(result);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "删除新闻")
    @ApiResponses({
          @ApiResponse(code = 200, message = "新闻删除成功"),
          @ApiResponse(code = 22005, message = "新闻不存在")
    })
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, paramType = "query", dataType = "int")
    @RequestMapping(value = "/deleteNewsById", method = RequestMethod.DELETE)
    public RestResponse deleteNewsByIds(@NotNull(message = "id不能不空") @Min(value = 1, message = "id最小值为1") Integer id) {
        newsService.deleteNewsByIds(id);
        return new RestResponse(CommonExceptionEnum.NEWS_DELETED_SUCCESSFULLY.getMsg());
    }

}
